package com.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pageResult.PageResult;
import com.pojo.CheckItem;
import com.query.QueryPageBean;
import com.result.Result;
import com.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    //检查项列表(支持分页+条件查询)
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = checkItemService.selectByQueryString(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    //检查项列表(暂时不支持分页)
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
    //删除
    @PostMapping("/delete")
    public Result delete(Integer id) throws CustomException {
            checkItemService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    //添加
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
    }

    //根据id查询
    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        try {
            CheckItem checkItem = checkItemService.queryById(id);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){
        try {
            checkItemService.update(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
    }




}
